#include "ZMysql.h"
#include <stdio.h>
#include "Util.h"
#include <memory>
#include <string.h>
#include <iostream>
using namespace std;
namespace mysqlcpp {
ZMysql::ZMysql() : is_connected_(false) {
	memset(&mysql_, 0, sizeof(mysql_));
	res_ = NULL;
	fd_ = NULL;
	//memset(row_, 0, sizeof(MYSQL_ROW));
	row_ = NULL;

	memset(host_, 0, 17);
	memset(user_, 0, 50);
	memset(db_, 0, 256);
	memset(password_, 0, 256);
	port_ = 0;
}

ZMysql::ZMysql(const char* host, unsigned short port, 
	const char* db, const char* user, 
	const char* password) : is_connected_(false) {
	if(host == NULL){
		//cout<<"host is NULL."<<endl;
		printf("FUNCTION:%s#host is NULL.\n",__FUNCTION__);
	}
	snprintf(host_, 17, "%s", host);

	port_ = port;

	if(db == NULL){
		//cout<<"db is NULL."<<endl;
		printf("FUNCTION:%s#db is NULL.\n",__FUNCTION__);
	}
	snprintf(db_, 256, "%s", db);

	if(user == NULL){
		//cout<<"user is NULL."<<endl;
		printf("FUNCTION:%s#user is NULL.\n",__FUNCTION__);
	}
	snprintf(user_, 50, "%s", user);

	if(password == NULL){
		//cout<<"password is NULL."<<endl;
		printf("FUNCTION:%s#password is NULL.\n",__FUNCTION__);
	}
	snprintf(password_, 256, "%s", password);
	memset(&mysql_, 0, sizeof(mysql_));
	res_ = NULL;
	//memset(row_, 0, sizeof(MYSQL_ROW));
	row_ = NULL;
	fd_ = NULL;

}
ZMysql::~ZMysql() {
	if (connected()) {
		disconnect();
	}
}

ulonglong ZMysql::affected_rows() {
	return mysql_affected_rows(&mysql_);
}

bool ZMysql::connect() {
	//cout<<"host:"<<host_<<"\n"<<"usr:"<<user_<<"\n";
	//cout<<"password:"<<password_<<"\n"<<"db:"<<db_<<"\n"<<"port:"<<port_<<endl;
	//&mysql_=mysql_init(NULL);
	MYSQL *mysql = mysql_init(NULL);
	mysql_ = *mysql;
	is_connected_ = mysql_real_connect(&mysql_, host_, user_, password_, db_, port_, NULL, 0);
	//is_connected_ = mysql_real_connect(mysql, host_, user_, password_, db_, port_, NULL, 0);
	//is_connected_ = mysql_real_connect(&mysql_, "127.0.0.1", "root", "sunkaisens", "lz", 3306, NULL, 0);
	if (!is_connected_) {
		//cout<<"connect error\n";
		printf("FUNCTION:%s#connect error.\n",__FUNCTION__);
		return false;
	}
	return true;
}

bool ZMysql::connect(const char* host, unsigned short port, 
	const char* db, const char* user, const char* password) {

	if (is_connected_)
		disconnect();
	//mysql_=mysql_init(NULL);
	MYSQL *mysql = mysql_init(NULL);
	mysql_ = *mysql;
	is_connected_ = mysql_real_connect(&mysql_, host, user, password, db, port, NULL, 0);
	if (!is_connected_) {
		//cout<<"connect error\n";
		printf("FUNCTION:%s#connect error.\n",__FUNCTION__);
		return false;
	}
	return true;
}

void ZMysql::disconnect() {
	if (is_connected_)
		mysql_close(&mysql_);
}


bool ZMysql::execute(const char* qstr, unsigned long strsize)  {
	return !mysql_real_query(&mysql_, qstr, strsize);
}

bool ZMysql::store_result() {
	res_ = mysql_store_result(&mysql_);
	if (!res_) {
		//cout<<"store_result error\n";
		printf("FUNCTION:%s#store_result error.\n",__FUNCTION__);
		return false;
	}
	return true;
}
bool ZMysql::use_result() {
	res_ = mysql_use_result(&mysql_);
	if (!res_) {
		//cout<<"use_result error\n";
		printf("FUNCTION:%s#use_result error.\n",__FUNCTION__);
		return false;
	}
	return true;
}
MYSQL_ROW ZMysql::fetch_row()  {
	row_ = mysql_fetch_row(res_);
	if (!row_) {
		//cout<<"fetch_row error."<<__FILE__<<endl;;
		printf("FUNCTION:%s#fetch_row error.\n",__FUNCTION__);
		
	}
	return row_;
} 
void ZMysql::free_result() {
	mysql_free_result(res_);
}
int ZMysql::num_fields() const {
	return mysql_num_fields(res_);


}
ulonglong ZMysql::num_rows() const {
	return mysql_num_rows(res_);
}	 
} // end namespace  mysqlcpp
